<template>
  <div>
    <Echart :options="options" id="carUnitChart" height="180px" width="320px"></Echart>
  </div>
</template>

<script>
import Echart from '@/common/echart'
export default {
  data () {
    return {
      options: {},
    };
  },
  components: {
    Echart,
  },
  props: {
    cdata: {
      type: Object,
      default: () => ({})
    },
  },
  watch: {
    cdata: {
      handler (newData) {
        this.options = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          legend: {
            data: ['总数', '值班数'],
            top: 0
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            type: 'category',
            data: newData.categories,
            axisLabel: {
              interval: 0, // 强制显示所有标签
              rotate: 0    // 标签旋转角度，可根据需要调整
            }
          },
          yAxis: {
            type: 'value',
            name: '数量',
            min: 0,
            // 确保y轴刻度为整数
            axisLabel: {
              formatter: '{value}'
            },
            // 网格线配置
            splitLine: {
              lineStyle: {
                type: 'dashed'
              }
            }
          },
          series: [
            {
              name: '总数',
              type: 'bar',
              data: newData.total,
              // 柱子样式
              itemStyle: {
                color: '#5470c6'
              },
              // 柱子宽度
              barWidth: '30%',
              // 显示数值标签
              label: {
                show: true, // 开启显示
                position: 'top', // 数值显示在柱子顶部
                formatter: '{c}', // 格式化显示内容，{c}表示数据值
                textStyle: {
                  color: '#FFF' // 数值颜色
                }
              }
            },
            {
              name: '值班数',
              type: 'bar',
              data: newData.onDuty,
              itemStyle: {
                color: '#91cc75'
              },
              barWidth: '30%',
              // 显示数值标签
              label: {
                show: true,
                position: 'top',
                formatter: '{c}',
                textStyle: {
                  color: '#FFF'
                }
              }
            }
          ]
        }
      },
      immediate: true,
      deep: true
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
